package org.edu.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.edu.model.Roles;
import org.edu.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@SessionScoped
@Controller
public class RolesController implements Serializable {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient RolesService rolesService;
	private Roles roles;
	private List<Roles> rolesList;

	@PostConstruct
	public void init() {
		rolesList = rolesService.roles();
	}

	public void preparedUpdateRoles(Long roles_id) throws Exception{
		roles = rolesService.findById(roles_id);
	}
	
	public String update(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			
			if(roles.getProfil_actions() == true){
				roles.setProfil_actions(true);
			}else if(roles.getProfil_actions() == false){
				roles.setProfil_actions(false);
			}

			if(roles.getAll_user() == true || roles.getAll_teacher() == true || roles.getAll_student() == true 
						|| roles.getAll_lesson() == true || roles.getSemester_lessons() == true || roles.getSemester_notes() == true){
				roles.setUser_actions(true);
			}else if(roles.getAll_user() == false && roles.getAll_teacher() == false && roles.getAll_student() == false 
					&& roles.getAll_lesson() == false && roles.getSemester_lessons() == false && roles.getSemester_notes() == false){
				roles.setUser_actions(false);
			}

			if(roles.getAll_project() == true || roles.getNotes_lesson() == true || roles.getNotes_project() == true){
				roles.setTeacher_affairs(true);
			}else if(roles.getAll_project() == false && roles.getNotes_lesson() == false && roles.getNotes_project() == false){
				roles.setTeacher_affairs(false);
			}

			if(roles.getChoose_lesson() == true || roles.getChoose_project() == true){
				roles.setStudent_affairs(true);
			}else if(roles.getChoose_lesson() == false && roles.getChoose_project() == false){
				roles.setStudent_affairs(false);
			}

			if(roles.getChange_password() == true || roles.getEdit_profil() == true || roles.getDelete_account() == true){
				roles.setAccount_settings(true);
			}else if(roles.getChange_password() == false && roles.getEdit_profil() == false && roles.getDelete_account() == false){
				roles.setAccount_settings(false);
			}
				
			
			rolesService.update(roles);
			facesContext.addMessage(null , new FacesMessage("Succesful" ,"Update Roles is Succesful"));
			init();
			roles = new Roles();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Error!" ,"Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public RolesController() {
		roles = new Roles();
	}

	public Roles getRoles() {
		if (roles == null) {
			roles = new Roles();
		}
		return roles;
	}

	public void setRoles(Roles roles) {
		this.roles = roles;
	}

	public List<Roles> getRolesList() {
		return rolesList;
	}

}
